package com.simple.gulimall.product.app;

import java.util.List;
import java.util.Map;

import com.simple.common.exception.CanNotDeleteEnum;
import com.simple.gulimall.product.entity.AttrEntity;
import com.simple.gulimall.product.service.AttrAttrgroupRelationService;
import com.simple.gulimall.product.service.AttrService;
import com.simple.gulimall.product.service.CategoryService;
import com.simple.gulimall.product.vo.AttrGroupRelationVo;
import com.simple.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.web.bind.annotation.*;

import com.simple.gulimall.product.entity.AttrGroupEntity;
import com.simple.gulimall.product.service.AttrGroupService;
import com.simple.common.utils.PageUtils;
import com.simple.common.utils.R;

import javax.annotation.Resource;


/**
 * 属性分组
 *
 * @author simpleZzz
 * @email 2466714892@qq.com
 * @date 2020-07-04 00:47:29
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Resource
    private AttrGroupService attrGroupService;
    @Resource
    private CategoryService categoryService;
    @Resource
    private AttrService attrService;
    @Resource
    private AttrAttrgroupRelationService relationService;

    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId){

        List<AttrEntity> attrEntities = attrService.getRelationAttr(attrgroupId);

        return R.ok().put("data",attrEntities);
    }
    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("catelogId") Long catelogId){
//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params,catelogId);

        return R.ok().put("page", page);
    }

//    {attrgroupId}/noattr/relation
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@RequestParam Map<String, Object> params,@PathVariable("attrgroupId") Long attrgroupId){

        PageUtils page = attrService.getNoRelationAttr(params, attrgroupId);

        return R.ok().put("page",page);
    }
    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long catelogId = attrGroup.getCatelogId();
        Long[] catelogPath = categoryService.findCatelogPath(catelogId);
        attrGroup.setCatelogPath(catelogPath);
        return R.ok().put("attrGroup", attrGroup);
    }
    /**
     * 获取分类下所有分组&关联属性
     */
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId){
        List<AttrGroupWithAttrsVo> attrGroupWithAttrsVos = attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
        return R.ok().put("data",attrGroupWithAttrsVos);
    }

    /**
     * 添加属性与分组的关联关系
     * /product/attrgroup/attr/relation
     */
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVo> vos){
        relationService.saveBatch(vos);
        return R.ok();
    }
    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除属性与分组的关联关系
     * @return
     */
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVo[] attrGroupRelationVos){
        attrGroupService.removeRelation(attrGroupRelationVos);

        return R.ok();
    }
    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		boolean removed = attrGroupService.removeGroupWithoutRelation(attrGroupIds);
        if (removed){
            return R.ok();
        }else {
            return R.error(CanNotDeleteEnum.HAVE_RELATION_EXCEPTION.getCode(),CanNotDeleteEnum.HAVE_RELATION_EXCEPTION.getMsg());
        }
    }

}
